import { ref } from 'vue'

/**
 * 按钮loading状态
 * @param {()=>Promise<any>} fn
 * @returns {[()=>Promise<any>, Vue.Ref<boolean>]}
 */
export const useLoadingBtn = (fn) => {
  const loading = ref(false)

  /**操作函数 */
  const handle = async function () {
    loading.value = true
    try {
      return await fn.apply(this, arguments)
    } catch (error) {
      console.error(error)
    } finally {
      loading.value = false
    }
  }
  return [handle, loading]
}
